import { AiEnvironment } from "../AiGameAction";
import { DecisionNode } from "../DecisionTree";
import DecisionCrystal from "./resouce/DecisionCrystal";
import DecisionOil from "./resouce/DecisionOil";

`
对资源的需求打分
通过目前等级能够建造的或升级的总数 与现有资源能够购买的数量 的比值打分

`
export default class DecisionResouce extends DecisionNode {

    environment: AiEnvironment & { lackCrystal: number, lackOil: number, lackAll: number };
    allLackCount: number;
    lackCrystalCount: number;
    lackOilCount: number;

    constructor(environment: any) {
        super(environment);
        this.childDecisionNodes.push(new DecisionCrystal(environment));
        this.childDecisionNodes.push(new DecisionOil(environment));
    }

    /**
     * 总分一百
     * 对资源的需求 
     * 当前所有功能或建造设施，所有资源都不足的数量 与 总数量之比
     * 缺少资源的 功能/设施 越多 得分越高
     * @returns 
     */
    evaluate(): number {
        let score = 10;
        if (this.environment.gameTime < 120) score += 10;
        this.allLackCount = 0,
            this.lackCrystalCount = 0,
            this.lackOilCount = 0;

        let addFunc = (item: boolean[]) => {
            if (item[2] && item[3]) {
                if (!item[0]) {
                    this.lackCrystalCount++;
                }
                if (!item[1]) {
                    this.lackOilCount++;
                }
                if (!item[0] && !item[1]) {
                    this.allLackCount++;
                }
            }
        }

        this.environment.gainFeasibility.forEach(addFunc, this);
        this.environment.facilityFeasibility.forEach(addFunc, this);
        return score + (this.lackCrystalCount * 10) + (this.lackOilCount * 10) + (this.allLackCount * 20);
    }


    execute(): number {
        this.environment.lackCrystal = this.lackCrystalCount;
        this.environment.lackOil = this.lackOilCount;
        this.environment.lackAll = this.allLackCount;
        return super.execute();
    }
}
